﻿
Option Strict On

Imports System.Net
Imports osi.root.utils
Imports osi.root.constants
Imports osi.root.formation
Imports osi.root.procedure
Imports osi.root.delegates
Imports osi.service.dns
Imports osi.service.tcp.constants

Public Class connection_manager
    Public Shared Function register(ByVal name As String,
                                    ByVal ppt As powerpoint) As Boolean
        If ppt Is Nothing OrElse Not is_valid_name(name) Then
            Return False
        Else
            Return l.writer_locked(Function() As Boolean
                                       Dim it As map(Of String, powerpoint).iterator = Nothing
                                       it = m.find(name)
                                       If it = m.end() Then
                                           m(name) = ppt
                                           Return True
                                       Else
                                           Return False
                                       End If
                                   End Function)
        End If
    End Function

    Private Shared Function register(ByVal name As String,
                                     ByVal p As _do(Of powerpoint, Boolean)) As Boolean
        assert(Not p Is Nothing)
        Dim ppt As powerpoint = Nothing
        Return p(ppt) AndAlso
               register(name, ppt)
    End Function

    Public Shared Function register(ByVal name As String,
                                    ByVal token As String,
                                    ByVal endpoint As IPEndPoint,
                                    ByVal connecting_timeout_ms As Int64,
                                    ByVal send_rate_sec As Int32,
                                    ByVal response_timeout_ms As Int64,
                                    ByVal receive_rate_sec As Int32,
                                    ByVal max_connecting As Int32,
                                    ByVal max_connected As Int32,
                                    ByVal no_delay As Boolean,
                                    ByVal max_lifetime_ms As Int64,
                                    ByVal is_outgoing As Boolean,
                                    ByVal enable_keepalive As Boolean,
                                    ByVal first_keepalive_ms As UInt32,
                                    ByVal keepalive_interval_ms As UInt32,
                                    ByVal zipper As String,
                                    ByVal encryptor As String) As Boolean
        Return register(name, Function(ByRef p As powerpoint) As Boolean
                                  Return powerpoint.create(token,
                                                           endpoint,
                                                           connecting_timeout_ms,
                                                           send_rate_sec,
                                                           response_timeout_ms,
                                                           receive_rate_sec,
                                                           max_connecting,
                                                           max_connected,
                                                           no_delay,
                                                           max_lifetime_ms,
                                                           is_outgoing,
                                                           enable_keepalive,
                                                           first_keepalive_ms,
                                                           keepalive_interval_ms,
                                                           zipper,
                                                           encryptor,
                                                           p)
                              End Function)
    End Function

    Public Shared Function register(ByVal name As String,
                                    ByVal token As String,
                                    ByVal port As UInt16,
                                    Optional ByVal send_rate_sec As Int32 = _
                                        default_value.send_rate_sec,
                                    Optional ByVal response_timeout_ms As Int64 = _
                                        default_value.response_timeout_ms,
                                    Optional ByVal receive_rate_sec As Int32 = _
                                        default_value.receive_rate_sec,
                                    Optional ByVal max_connecting As Int32 = _
                                        default_value.max_connecting,
                                    Optional ByVal max_connected As Int32 = _
                                        default_value.incoming.max_connected,
                                    Optional ByVal no_delay As Boolean = _
                                        default_value.no_delay,
                                    Optional ByVal max_lifetime_ms As Int64 = _
                                        default_value.incoming.max_lifetime_ms,
                                    Optional ByVal enable_keepalive As Boolean = _
                                        default_value.enable_keepalive,
                                    Optional ByVal first_keepalive_ms As UInt32 = _
                                        default_value.first_keepalive_ms,
                                    Optional ByVal keepalive_interval_ms As UInt32 = _
                                        default_value.keepalive_interval_ms,
                                    Optional ByVal zipper As String = _
                                        default_value.zipper,
                                    Optional ByVal encryptor As String = _
                                        default_value.encryptor) _
                                   As Boolean
        Return register(name, Function(ByRef p As powerpoint) As Boolean
                                  Return powerpoint.create(token,
                                                           port,
                                                           p,
                                                           send_rate_sec,
                                                           response_timeout_ms,
                                                           receive_rate_sec,
                                                           max_connecting,
                                                           max_connected,
                                                           no_delay,
                                                           max_lifetime_ms,
                                                           enable_keepalive,
                                                           first_keepalive_ms,
                                                           keepalive_interval_ms,
                                                           zipper,
                                                           encryptor)
                              End Function)
    End Function

    Public Shared Function register(ByVal name As String,
                                    ByVal port As UInt16,
                                    Optional ByVal send_rate_sec As Int32 = _
                                        default_value.send_rate_sec,
                                    Optional ByVal response_timeout_ms As Int64 = _
                                        default_value.response_timeout_ms,
                                    Optional ByVal receive_rate_sec As Int32 = _
                                        default_value.receive_rate_sec,
                                    Optional ByVal max_connecting As Int32 = _
                                        default_value.max_connecting,
                                    Optional ByVal max_connected As Int32 = _
                                        default_value.outgoing.max_connected,
                                    Optional ByVal no_delay As Boolean = _
                                        default_value.no_delay,
                                    Optional ByVal max_lifetime_ms As Int64 = _
                                        default_value.incoming.max_lifetime_ms,
                                    Optional ByVal enable_keepalive As Boolean = _
                                        default_value.enable_keepalive,
                                    Optional ByVal first_keepalive_ms As UInt32 = _
                                        default_value.first_keepalive_ms,
                                    Optional ByVal keepalive_interval_ms As UInt32 = _
                                        default_value.keepalive_interval_ms,
                                    Optional ByVal zipper As String = _
                                        default_value.zipper,
                                    Optional ByVal encryptor As String = _
                                        default_value.encryptor) _
                                   As Boolean
        Return register(name,
                        name,
                        port,
                        send_rate_sec,
                        response_timeout_ms,
                        receive_rate_sec,
                        max_connecting,
                        max_connected,
                        no_delay,
                        max_lifetime_ms,
                        enable_keepalive,
                        first_keepalive_ms,
                        keepalive_interval_ms,
                        zipper,
                        encryptor)
    End Function

    Public Shared Function register(ByVal name As String,
                                    ByVal token As String,
                                    ByVal remote_endpoint As IPEndPoint,
                                    Optional ByVal connecting_timeout_ms As Int64 = _
                                        default_value.outgoing.connecting_timeout_ms,
                                    Optional ByVal send_rate_sec As Int32 = _
                                        default_value.send_rate_sec,
                                    Optional ByVal response_timeout_ms As Int64 = _
                                        default_value.response_timeout_ms,
                                    Optional ByVal receive_rate_sec As Int32 = _
                                        default_value.receive_rate_sec,
                                    Optional ByVal max_connecting As Int32 = _
                                        default_value.max_connecting,
                                    Optional ByVal max_connected As Int32 = _
                                        default_value.outgoing.max_connected,
                                    Optional ByVal no_delay As Boolean = _
                                        default_value.no_delay,
                                    Optional ByVal max_lifetime_ms As Int64 = _
                                        default_value.outgoing.max_lifetime_ms,
                                    Optional ByVal enable_keepalive As Boolean = _
                                        default_value.enable_keepalive,
                                    Optional ByVal first_keepalive_ms As UInt32 = _
                                        default_value.first_keepalive_ms,
                                    Optional ByVal keepalive_interval_ms As UInt32 = _
                                        default_value.keepalive_interval_ms,
                                    Optional ByVal zipper As String = _
                                        default_value.zipper,
                                    Optional ByVal encryptor As String = _
                                        default_value.encryptor) _
                                   As Boolean
        Return register(name, Function(ByRef p As powerpoint) As Boolean
                                  Return powerpoint.create(token,
                                                           remote_endpoint,
                                                           p,
                                                           connecting_timeout_ms,
                                                           send_rate_sec,
                                                           response_timeout_ms,
                                                           receive_rate_sec,
                                                           max_connecting,
                                                           max_connected,
                                                           no_delay,
                                                           max_lifetime_ms,
                                                           enable_keepalive,
                                                           first_keepalive_ms,
                                                           keepalive_interval_ms,
                                                           zipper,
                                                           encryptor)
                              End Function)
    End Function

    Public Shared Function register(ByVal name As String,
                                    ByVal remote_endpoint As IPEndPoint,
                                    Optional ByVal connecting_timeout_ms As Int64 = _
                                        default_value.outgoing.connecting_timeout_ms,
                                    Optional ByVal send_rate_sec As Int32 = _
                                        default_value.send_rate_sec,
                                    Optional ByVal response_timeout_ms As Int64 = _
                                        default_value.response_timeout_ms,
                                    Optional ByVal receive_rate_sec As Int32 = _
                                        default_value.receive_rate_sec,
                                    Optional ByVal max_connecting As Int32 = _
                                        default_value.max_connecting,
                                    Optional ByVal max_connected As Int32 = _
                                        default_value.outgoing.max_connected,
                                    Optional ByVal no_delay As Boolean = _
                                        default_value.no_delay,
                                    Optional ByVal max_lifetime_ms As Int64 = _
                                        default_value.outgoing.max_lifetime_ms,
                                    Optional ByVal enable_keepalive As Boolean = _
                                        default_value.enable_keepalive,
                                    Optional ByVal first_keepalive_ms As UInt32 = _
                                        default_value.first_keepalive_ms,
                                    Optional ByVal keepalive_interval_ms As UInt32 = _
                                        default_value.keepalive_interval_ms,
                                    Optional ByVal zipper As String = _
                                        default_value.zipper,
                                    Optional ByVal encryptor As String = _
                                        default_value.encryptor) _
                                   As Boolean
        Return register(name,
                        name,
                        remote_endpoint,
                        connecting_timeout_ms,
                        send_rate_sec,
                        response_timeout_ms,
                        receive_rate_sec,
                        max_connecting,
                        max_connected,
                        no_delay,
                        max_lifetime_ms,
                        enable_keepalive,
                        first_keepalive_ms,
                        keepalive_interval_ms,
                        zipper,
                        encryptor)
    End Function

    Public Shared Function register_outgoing_connection(
                                ByVal name As String,
                                ByVal token As String,
                                ByVal address As String,
                                ByVal port As UInt16,
                                Optional ByVal connecting_timeout_ms As Int64 = _
                                    default_value.outgoing.connecting_timeout_ms,
                                Optional ByVal send_rate_sec As Int32 = _
                                    default_value.send_rate_sec,
                                Optional ByVal response_timeout_ms As Int64 = _
                                    default_value.response_timeout_ms,
                                Optional ByVal receive_rate_sec As Int32 = _
                                    default_value.receive_rate_sec,
                                Optional ByVal max_connecting As Int32 = _
                                    default_value.max_connecting,
                                Optional ByVal max_connected As Int32 = _
                                    default_value.outgoing.max_connected,
                                Optional ByVal no_delay As Boolean = _
                                    default_value.no_delay,
                                Optional ByVal max_lifetime_ms As Int64 = _
                                    default_value.outgoing.max_lifetime_ms,
                                Optional ByVal ipv4 As Boolean = _
                                    default_value.ipv4,
                                Optional ByVal enable_keepalive As Boolean = _
                                    default_value.enable_keepalive,
                                Optional ByVal first_keepalive_ms As UInt32 = _
                                    default_value.first_keepalive_ms,
                                Optional ByVal keepalive_interval_ms As UInt32 = _
                                    default_value.keepalive_interval_ms,
                                Optional ByVal zipper As String = _
                                    default_value.zipper,
                                Optional ByVal encryptor As String = _
                                    default_value.encryptor) As Boolean
        Return register(name, Function(ByRef r As powerpoint) As Boolean
                                  Return powerpoint.create(token,
                                                           address,
                                                           port,
                                                           r,
                                                           connecting_timeout_ms,
                                                           send_rate_sec,
                                                           response_timeout_ms,
                                                           receive_rate_sec,
                                                           max_connecting,
                                                           max_connected,
                                                           no_delay,
                                                           max_lifetime_ms,
                                                           ipv4,
                                                           enable_keepalive,
                                                           first_keepalive_ms,
                                                           keepalive_interval_ms,
                                                           zipper,
                                                           encryptor)
                              End Function)
    End Function

    Public Shared Function register_outgoing_connection(
                                ByVal name As String,
                                ByVal address As String,
                                ByVal port As UInt16,
                                Optional ByVal connecting_timeout_ms As Int64 = _
                                    default_value.outgoing.connecting_timeout_ms,
                                Optional ByVal send_rate_sec As Int32 = _
                                    default_value.send_rate_sec,
                                Optional ByVal response_timeout_ms As Int64 = _
                                    default_value.response_timeout_ms,
                                Optional ByVal receive_rate_sec As Int32 = _
                                    default_value.receive_rate_sec,
                                Optional ByVal max_connecting As Int32 = _
                                    default_value.max_connecting,
                                Optional ByVal max_connected As Int32 = _
                                    default_value.outgoing.max_connected,
                                Optional ByVal no_delay As Boolean = _
                                    default_value.no_delay,
                                Optional ByVal max_lifetime_ms As Int64 = _
                                    default_value.outgoing.max_lifetime_ms,
                                Optional ByVal ipv4 As Boolean = _
                                    default_value.ipv4,
                                Optional ByVal enable_keepalive As Boolean = _
                                    default_value.enable_keepalive,
                                Optional ByVal first_keepalive_ms As UInt32 = _
                                    default_value.first_keepalive_ms,
                                Optional ByVal keepalive_interval_ms As UInt32 = _
                                    default_value.keepalive_interval_ms,
                                Optional ByVal zipper As String = _
                                    default_value.zipper,
                                Optional ByVal encryptor As String = _
                                    default_value.encryptor) As Boolean
        Return register_outgoing_connection(name,
                                            name,
                                            address,
                                            port,
                                            connecting_timeout_ms,
                                            send_rate_sec,
                                            response_timeout_ms,
                                            receive_rate_sec,
                                            max_connecting,
                                            max_connected,
                                            no_delay,
                                            max_lifetime_ms,
                                            ipv4,
                                            enable_keepalive,
                                            first_keepalive_ms,
                                            keepalive_interval_ms,
                                            zipper,
                                            encryptor)
    End Function

    Public Shared Function retire(ByVal name As String) As Boolean
        Dim p As powerpoint = Nothing
        If [get](name, p) Then
            assert(Not p Is Nothing)
            p.close()
            Return l.writer_locked(Function() As Boolean
                                       Return m.erase(name)
                                   End Function)
        Else
            Return False
        End If
    End Function
End Class
